﻿#include "FITKAbaqusHDF5AdaptorDashpotPoint2Point.h"
#include "FITK_Kernel/FITKCore/FITKEnumTransformer.hpp"
#include "FITK_Interface/FITKInterfaceIO/FITKAbstractHDF5Reader.h"
#include "FITK_Interface/FITKInterfaceIO/FITKAbstractHDF5Writer.h"
#include "FITK_Interface/FITKInterfaceModel/FITKCoordinateSystem.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaAbstractSpringDashpot.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSpringDashpotPoint2Point.h"

namespace IO
{
    QString FITKAbaqusHDF5AdaptorDashpotPoint2Point::getAdaptorClass()
    {
        return "FITKAbaqusHDF5AdaptorDashpotPoint2Point";
    }

    bool FITKAbaqusHDF5AdaptorDashpotPoint2Point::adaptR()
    {
        auto sd = dynamic_cast<Interface::FITKAbaAbstractSpringDashpot*>(_dataObj);
        if (!_reader || !sd || !_h5Group) return false;
        bool isR = true;
        isR &= readDashpot(sd, *_h5Group);
        return isR;
    }

    bool FITKAbaqusHDF5AdaptorDashpotPoint2Point::adaptW()
    {
        auto sd = dynamic_cast<Interface::FITKAbaAbstractSpringDashpot*>(_dataObj);
        if (!_writer || !sd || !_h5Group) return false;
        bool isW = true;
        isW &= writeDashpot(sd, *_h5Group);
        return isW;
    }


    bool FITKAbaqusHDF5AdaptorDashpotPoint2Point::readDashpot(Interface::FITKAbaAbstractSpringDashpot * sd, H5::Group & h5Group)
    {
        if (!sd) return false;
        bool isR = true;

        isR &= FITKAbaqusHDF5AdaptorAbaWire::readAbaWire(sd, h5Group);

        double springStiffness = readDoubleAttribute(h5Group, "SpringStiffness");
        sd->setSpringStiffness(springStiffness);
        double daspotCoefficent = readDoubleAttribute(h5Group, "DaspotCoefficent");
        sd->setDaspotCoefficent(daspotCoefficent);
        bool stiffnessState = readBoolAttribute(h5Group, "StiffnessState");
        sd->setStiffnessState(stiffnessState);
        bool daspotState = readBoolAttribute(h5Group, "DaspotState");
        sd->setDaspotState(daspotState);

        auto eSpringDashpotType = sd->getSpringDashpotType();
        if (eSpringDashpotType == Interface::FITKAbaAbstractSpringDashpot::SpringDashpotType::ConnectTwoPoints)
        {
            isR &= readDashpotPoint2Point(sd, _h5CreateGroup);
        }
        else
            return false;

        return true;
    }

    bool FITKAbaqusHDF5AdaptorDashpotPoint2Point::writeDashpot(Interface::FITKAbaAbstractSpringDashpot* sd, H5::Group& h5Group)
    {
        if (!sd) return false;
        bool isW = true;
        bool isValid = false;

        QString name = sd->getDataObjectName();

        isW &= FITKAbaqusHDF5AdaptorAbaWire::writeAbaWire(sd, h5Group);
        if (!isW) return false;

        auto eSpringDashpotType = sd->getSpringDashpotType();

        //枚举转换字符
        Core::FITKEnumTransfer<Interface::FITKAbaAbstractSpringDashpot::SpringDashpotType> fitkTypeTrafer;
        auto sSpringDashpotType = fitkTypeTrafer.toStrting(eSpringDashpotType, isValid);
        if (!isValid) return false;
        writeStrAttribute(_h5CreateGroup, "Type", sSpringDashpotType.toStdString());
        double springStiffness = sd->getSpringStiffness();
        writeDoubleAttribute(_h5CreateGroup, "SpringStiffness", &springStiffness);
        double daspotCoefficent = sd->getDaspotCoefficent();
        writeDoubleAttribute(_h5CreateGroup, "DaspotCoefficent", &daspotCoefficent);
        bool stiffnessState = sd->getStiffnessState();
        writeBoolAttribute(_h5CreateGroup, "StiffnessState", &stiffnessState);
        bool daspotState = sd->getDaspotState();
        writeBoolAttribute(_h5CreateGroup, "DaspotState", &daspotState);

        if (eSpringDashpotType == Interface::FITKAbaAbstractSpringDashpot::SpringDashpotType::ConnectTwoPoints)
        {
            isW &= writeDashpotPoint2Point(sd, _h5CreateGroup);
        }
        else
            return false;

        return true;
    }

    bool FITKAbaqusHDF5AdaptorDashpotPoint2Point::readDashpotPoint2Point(Interface::FITKAbaAbstractSpringDashpot* sd, H5::Group& h5Group)
    {
        auto p2pSd = dynamic_cast<Interface::FITKAbaSpringDashpotPoint2Point*>(sd);
        if (!p2pSd) return false;
        return true;
    }

    bool FITKAbaqusHDF5AdaptorDashpotPoint2Point::writeDashpotPoint2Point(Interface::FITKAbaAbstractSpringDashpot * sd, H5::Group & h5Group)
    {
        auto p2pSd = dynamic_cast<Interface::FITKAbaSpringDashpotPoint2Point*>(sd);
        if (!p2pSd) return false;
        return true;
    }

}


